<template>
	<view class="work-order full box relative flex-col bg-primary">
		<view class="tab-wrap">
			<u-tabs :list="tabList" :bar-style="{bottom:'-4px'}" :is-scroll="false" v-model="current"
				@change="handleTabChange" :bold="false"></u-tabs>
		</view>
		<view class="w-wrap">
			<view class="w-inner">
				<mescroll-uni @init="mescrollInit" :fixed="false" @down="downCallback" @up="upCallback">
					<view class="record-list">
						<view class="record-item" v-for="(item,index) in list" :key="index"
							@click="handleToDetail(item)">
							<view class="item-header">
								<text class="order-no">工单编号：{{ item.sn }}</text>
								<text class="item-status">{{ item.status_text }}</text>
							</view>
							<view class="item-title">
								<text>{{ item.title }}</text>
							</view>
							<view class="item-desc">
								<text>联系方式：{{ item.mobile }}</text>
							</view>
							<view class="item-desc">
								<text>时间：{{ item.date }}</text>
							</view>
							<view class="item-desc">
								<text>地址：{{ item.xiangxi_address }}</text>
							</view>
							<view class="order-price">
								<text>订单总额：￥{{ item.order_amount }}</text>
							</view>
							<view class="item-btns">
								<text v-if="item.status === 10" class="btn-item" @click.stop="handlePay(item)">去支付</text>
								<text class="btn-item" v-else>查看详情</text>
							</view>
						</view>
					</view>
				</mescroll-uni>
			</view>
		</view>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
  import {request, showLoading, toast} from "@/utils/tool";
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				list: [],
				tabList: [{
						name: '待付款',
					},
					{
						name: '待服务',
					}, {
						name: '服务中'
					}, {
						name: '已完成'
					},
					{
						name: '已取消'
					}
				],
				current: 0
			}
		},
		methods: {
      handleUpdate(){
        this.mescroll.triggerDownScroll()
      },
      async prePay(data, openid, hide) {
        const { from, order_id } = data
        const resp = await request({
          url: "/pay/prepay",
          data: {
            from,
            pay_way: 2,
            order_id,
            pay_qudao: 1,
            openid
          }
        }).catch(hide)
        if (resp.code !== 1) {
          hide()
          return toast({ title: resp.msg || "获取支付参数失败" })
        }
        const { config } = resp.data
        hide()
        uni.requestPayment({
          ...config,
          success: ev => {
            toast({ title: "支付成功", icon: "success" })
            this.mescroll.triggerDownScroll()
          },
          fail: error => {
            toast({ title: "支付失败" })
          }
        })
      },
      getOpenId() {
        return new Promise(resolve => {
          uni.login({
            provider: "weixin",
            success: async ev => {
              const { code } = ev
              const resp = await request({
                url: '/login/mnpGetOpenid',
                data: { code }
              })
              resolve([null, resp.data])
            },
            fail: error => {
              toast({ title: "获取code失败" })
              resolve([error, null])
            }
          })

        })
      },
      async handlePay(rowItem){
        const hide = showLoading("正在获取")
        const {id} = rowItem
        const [codeError, codeResp] = await this.getOpenId()
        if (codeError) return hide()
        const { openid } = codeResp
       await this.prePay({from:"gongdan",order_id:id}, openid, hide)
      },
			handleToDetail(data) {
				uni.navigateTo({
					url: "/pages/mine/work-order/order-detail?data=" + JSON.stringify(data),
				})
			},
			handleTabChange(ev) {
				this.current = ev
				this.mescroll.triggerDownScroll()
			},
			async upCallback(ev) {
				const { num, size } = ev
				const { current } = this
				const statusEnum = {
					0: 10,
					1: 20,
					2: 30,
					3: 40,
					4: 50
				}
				const resp = await request({
					url: "/gongdan/lists",
					data: {
						page_no: num,
						page_size: size,
						status: statusEnum[current] || ""
					},
					method: "GET"
				})
				const { lists, count } = resp.data
				if (num === 1) this.list = []
				this.list.push(...lists)
				this.mescroll.endBySize(lists.length, count)
			}
		},
    created() {
      uni.$on("workOrderPaySuccess",this.handleUpdate)
    },
    destroyed() {
      uni.$off("workOrderPaySuccess",this.handleUpdate)
    },
	}
</script>

<style lang="scss" scoped>
	.work-order {
		.tab-wrap {
			border-bottom: 2rpx solid #e8e8e8;
		}

		.record-list {
			box-sizing: border-box;
			position: relative;
			padding: 26rpx 32rpx;

			.record-item {
				padding: 32rpx;
				background: #FFFFFF;
				box-sizing: border-box;
				position: relative;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				margin-bottom: 16rpx;

				&:last-child {
					margin-bottom: 0;
				}

				.item-btns {
					margin-top: 16rpx;
					display: flex;
					align-items: center;
					justify-content: flex-end;

					.btn-item {
						padding: 10rpx 24rpx;
						background: #FFFFFF;
						border-radius: 200rpx 200rpx 200rpx 200rpx;
						border: 2rpx solid #3361F0;
						font-weight: bold;
						font-size: 28rpx;
						color: #3361F0;
						line-height: 44rpx;
						margin-left: 20rpx;
					}
				}

				.order-price {
					margin-top: 16rpx;
					padding-top: 16rpx;
					border-top: 2rpx solid #E5E6EB;
					font-weight: bold;
					font-size: 24rpx;
					color: #1D2129;
				}

				.item-desc {
					font-weight: 400;
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.6);
					line-height: 40rpx;
					margin-top: 8rpx;
				}

				.item-title {
					font-weight: bold;
					font-size: 32rpx;
					color: #3B3B3B;
					line-height: 44rpx;
					margin-top: 16rpx;
				}

				.item-header {
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding-bottom: 16rpx;
					border-bottom: 2rpx solid #E5E6EB;

					.order-no {
						font-weight: bold;
						font-size: 28rpx;
						color: #1D2129;
						line-height: 44rpx;
					}

					.item-status {
						font-weight: bold;
						font-size: 24rpx;
						color: #3361F0;
						line-height: 40rpx;
					}
				}
			}
		}
	}
</style>